@extends('admin.layout.base')
@section('title', '添加商品')
@section('pluginstyle')
<link rel="stylesheet" type="text/css" href="{{ asset('/plugins/webuploader/webuploader.css') }}">
<style type="text/css">
    .attr-box{padding-left: 0px;}
</style>
@endsection
@section('content')
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            添加商品
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin') }}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="{{ url('/admin/goods') }}">商品管理</a></li>
            <li class="active">添加商品</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <!-- right column -->
            <div class="col-md-12">
                <!-- Horizontal Form -->
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">添加分类</h3>
                    </div>
                    <!-- /.box-header -->
                    @if(session('errors'))
                    <div class="alert alert-danger alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                        @foreach($errors->all() as $key => $error)
                        {{ $key+1 . ':' . $error }}
                        @endforeach
                    </div>
                    @endif
                    <form class="form-horizontal" action="{{ url('/admin/goods') }}" method="post" enctype="multipart/form-data">
                        {!! csrf_field() !!}
                        <div class="box-body">
                            <div class="nav-tabs-custom">
                                <ul class="nav nav-tabs">
                                    <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">基本信息</a></li>
                                </ul>

                                <div class="tab-content">
                                    <div class="tab-pane active" id="tab_1">
                                        <div class="form-group">
                                            <label for="goods_name" class="col-sm-2 control-label">商品名称</label>
                                            <div class="col-sm-5">
                                                <input type="text" class="form-control" id="goods_name" name="goods_name" value="{{ old('goods_name') }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="cate_id" class="col-sm-2 control-label">所属分类</label>
                                            <div class="col-sm-5">
                                                <select name="cate_id" id="cate_id" class="form-control">
                                                    <option value="">-- 请选择分类 --</option>
                                                    @foreach($category as $key => $cate)
                                                    @if($key == 0)
                                                    <optgroup label="{{ $cate['cate_name'] }}">
                                                    @elseif($cate['level'] == 0)
                                                    </optgroup><optgroup label="{{ $cate['cate_name'] }}">
                                                    @else
                                                    <option value="{{ $cate['id'] }}">{{ str_repeat('&nbsp;&nbsp;', $cate['level']*2) }}{{ $cate['cate_name'] }}</option>
                                                    @endif
                                                    @endforeach
                                                    </optgroup>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_price" class="col-sm-2 control-label">商品价格</label>
                                            <div class="col-sm-3">
                                                <input type="text" class="form-control" id="goods_price" name="goods_price" value="{{ old('goods_price', '0.00') }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_num" class="col-sm-2 control-label">商品库存</label>
                                            <div class="col-sm-3">
                                                <input type="text" class="form-control" id="goods_num" name="goods_num" value="{{ old('goods_num', '0') }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_brief" class="col-sm-2 control-label">商品简述</label>
                                            <div class="col-sm-7">
                                                <input type="text" class="form-control" id="goods_brief" name="goods_brief" value="{{ old('goods_brief') }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_img" class="col-sm-2 control-label">商品属性</label>
                                            <div class="col-sm-10">
                                                <button class="btn btn-info" id="attr-add" type="button"><i class="fa fa-plus"></i>增加属性</button>
                                                <button class="btn btn-warning" id="attr-remove" type="button"><i class="fa fa-trash-o"></i>删除属性</button>
                                                <hr />
                                            </div>
                                            <div class="col-sm-5 col-sm-offset-2 attr-box">
                                                <div class="goods-attr">
                                                    <div class="col-sm-6">
                                                        <input type="text" name="attr_type[]" id="" class="form-control" placeholder="属性名称" value=""/>
                                                    </div>
                                                    <div class="col-sm-6">
                                                        <input type="text" name="attr_price[]" id="" class="form-control" placeholder="价格"/>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_img" class="col-sm-2 control-label">商品图片</label>
                                            <div class="col-sm-5">
                                                <div id="goods">选择图片</div>
                                                <img src="{{ old('goods_thumb') }}" alt="" class="img-thumbnail" />
                                                <input type="hidden" class="form-control" id="goods_img" name="goods_img" value="{{ old('goods_img') }}">
                                                <input type="hidden" class="form-control" id="goods_thumb" name="goods_thumb" value="{{ old('goods_thumb') }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_spec" class="col-sm-2 control-label">规格参数</label>
                                            <div class="col-sm-10">
                                                <textarea name="goods_spec" id="goods_spec" class="" rows="10">{{ old('goods_spec') }}</textarea>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_desc" class="col-sm-2 control-label">商品详情</label>
                                            <div class="col-sm-10">
                                                <textarea name="goods_desc" id="goods_desc" class="" rows="10">{{ old('goods_desc') }}</textarea>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.tab-pane -->
                                </div>
                                <!-- /.tab-content -->
                            </div>
                            <button type="button" class="btn btn-default" onclick="history.go(-1)">返回</button>
                            <button type="submit" class="btn btn-info pull-right">添加</button>
                        </div>
                    </form>
                    <!-- form start -->
                </div>
                <!-- /.box -->
            </div>
            <!--/.col (right) -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
</div>
@endsection
@section('pluginscript')
<!-- FastClick -->
<script src="{{ asset('/dist/admin/plugins/fastclick/fastclick.js') }}"></script>
<script type="text/javascript" src="{{ asset('/plugins/webuploader/webuploader.js') }}"></script>
<script type="text/javascript" src="{{ asset('/common/js/upload.js') }}"></script>
<script type="text/javascript">
$(function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    uploadone('#goods');
});
</script>
<script type="text/javascript" charset="utf-8" src="{{ asset('/plugins/ueditor/ueditor.config.js') }}"></script>
<script type="text/javascript" charset="utf-8" src="{{ asset('/plugins/ueditor/ueditor.all.min.js') }}"></script>
<script>
$(function() {
    // 实例化文本编辑器
    var content = UE.getEditor('goods_desc');
    var spec = UE.getEditor('goods_spec');

    // 添加属性
    var str = $('.attr-box .goods-attr').html();
    $('#attr-add').click(function() {
        var attrBox = $('.attr-box');
        var html = '<div class="goods-attr">' + str + '</div>';
        attrBox.append(html);
    });

    // 减少属性
    $('#attr-remove').click(function() {
        var attrBox = $('.attr-box');
        if (attrBox.children('.goods-attr').length <= 1) {
            return false;
        } else {
            attrBox.find('.goods-attr').last().remove();
        }
    });
});
</script>
@endsection